var booklet = {
    position : -1,
    byPage : 2,
    elm : null,
    book_page : '.b-page-',
    isMobile : false,
    isFancyLoad : false,
    FancyArray : new Array(),
    order : true,
    init : function(elm, start){


        var hashPage = this.getHashNum();
        pageS = hashPage;

        if(start)
            pageS = start;
        
        if(pageS)
            this.position = pageS + this.position +  this.byPage * -1;


        
        this.mobile();
        this.elm = elm;
        this.next();
        this.elm.find('.b-prev').click(function (){booklet.prev();})
        this.elm.find('.b-next').click(function (){booklet.next();})
    },
    load : function(){
        var n1 = this.position;
        var n2 = this.position +1;

        var elm1 = this.elm.find(this.book_page+n1).find('a.toPic');
        var elm2 = this.elm.find(this.book_page+n2).find('a.toPic');
        elm1.parent().append('<a class="fancy" href="'+elm1.attr('href')+'"><img src="'+elm1.attr('href')+'" class="'+elm1.attr('class')+'"/></a>');
        elm2.parent().append('<a class="fancy" href="'+elm2.attr('href')+'"><img src="'+elm2.attr('href')+'" class="'+elm2.attr('class')+'" /></a>');
        if(!this.isMobile) { // Pas de mobile ? => lightbox au click
            if(this.isFancyLoad){
                elm1.parent().find('.fancy').fancybox();
                elm2.parent().find('.fancy').fancybox();
            }else {
                this.FancyArray.push(elm1.parent().find('.fancy'));
                this.FancyArray.push(elm2.parent().find('.fancy'));
            }
        }else { // Mobile = pas d'action au click
            elm1.parent().find('.fancy').click(function (e) {e.preventDefault();});
        }

        elm1.remove();
        elm2.remove();

    },
    next : function(e){
        this.order = true;
        this.position+=this.byPage;
        this.load();
    },
    prev : function(){
        this.order = false;
        this.position-=this.byPage;
        this.load();
    },
    mobile : function (){   // Detection des mobiles
        var agent = navigator.userAgent.toLowerCase();
        var otherBrowser = (agent.indexOf("series60") != -1) || (agent.indexOf("symbian") != -1) || (agent.indexOf("windows ce") != -1) || (agent.indexOf("blackberry") != -1);
        var mobileOS = typeof orientation != 'undefined' ? true : false;
        var touchOS = ('ontouchstart' in document.documentElement) ? true : false;
        var iOS = (navigator.platform.indexOf("iPhone") != -1) ||
                (navigator.platform.indexOf("iPad") != -1) ? true : false;
        var android = (agent.indexOf("android") != -1) || (!iOS && !otherBrowser && touchOS && mobileOS) ? true : false;
        var webOS =  (agent.indexOf("webos") != -1);

        if(webOS) mobileOS = true;

        this.isMobile = mobileOS;
        if(mobileOS) $('body').addClass('mobile');
        if(otherBrowser) $('body').addClass(otherBrowser);
        if(touchOS) $('body').addClass('touch');
        if(iOS) $('body').addClass('iOS');
        if(android) $('body').addClass('android');
        if(webOS) $('body').addClass('webOS');

        if(!mobileOS)
            this.lightbox_init();
        else
            $(this.elm).find('.firstPage').click(function (e) {e.preventDefault();});

    },
    lightbox_init : function (){ // Chargement des scripts lightbox uniquement en cas de besoin != mobile
        $("head").append("<link rel='stylesheet' type='text/css' href='css/fancybox/jquery.fancybox-1.3.4.css' />");
        $.getScript('js/fancybox/jquery.fancybox-1.3.4.pack.js', function(data, textStatus){
            $.getScript('js/fancybox/jquery.easing-1.3.pack.js', function(data, textStatus){
                $.getScript('js/fancybox/jquery.mousewheel-3.0.4.pack.js', function(data, textStatus){
                    booklet.isFancyLoad = true;
                    $(booklet.elm).find('.firstPage').fancybox();
                    $.each(booklet.FancyArray, function (i, item){
                        $(item).fancybox();
                    })
                });
            });
        });
    },
    //get page number from hash tag, last element
    getHashNum : function (){
        var hash = window.location.hash.split('/');
        if(hash.length > 1){
            return parseInt(hash[2]);
        }else{
            return false;
        }
    }

}

$(document).ready(function() {
   // var start = 5;
    var mybook = $('#mybook').booklet({
        arrows: true,
        width: 900,
        height: 600,
        hash: true,
      //  startingPage: start,
        after: function (){
            $('.booklet .b-arrow div').css('opacity', 0.35);
            // Pré-chargement des futurs pages
            if(booklet.order)
                booklet.next();
            else
                booklet.prev();
        }
    });
    booklet.init($('#mybook')/*, start*/);
});